package katris.selenium.tests.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
import katris.selenium.tests.model.Film;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AddNewDescriptionPage extends InternalPage {
	
	public AddNewDescriptionPage(PageManager pages) {
		super(pages);
	}

	  public AddNewDescriptionPage ensurePageLoaded() {
		    super.ensurePageLoaded();
		    wait.until(visibilityOfElementLocated(By.xpath("//div[@class='addmovie']")));
		    return this;
		  }
	  
	  
	  public void insertMovieData(Film film) {
          
	    	  WebElement titleInsert = wait.until(elementToBeClickable(By.xpath("//input[@name='name']")));
	    	  titleInsert.click();
	    	  titleInsert.sendKeys(Keys.CONTROL + "a");
	    	  titleInsert.sendKeys(Keys.DELETE);
	    	  titleInsert.sendKeys(film.getTitle());

	    	  WebElement yearInsert = wait.until(elementToBeClickable(By.xpath("//input[@name='year']")));
	    	  yearInsert.click();
	    	  yearInsert.sendKeys(Keys.CONTROL + "a");
	    	  yearInsert.sendKeys(Keys.DELETE);
	    	  yearInsert.sendKeys(film.getYear());

	    	  
	    }
	  
	  
	  public boolean checkWarningsForMandatoryFields() {

	            WebElement field1 = new WebDriverWait(driver,10).until(presenceOfElementLocated(By.xpath("//label[@for='name']")));
	              if (field1.isDisplayed()) {
	            	  System.out.println("The mandatory field Title is empty!!!");
	                 return false;
	              }

	            WebElement field2 = new WebDriverWait(driver,10).until(presenceOfElementLocated(By.xpath("//label[@for='year']")));
	            if (field2.isDisplayed()) {
	            	System.out.println("The mandatory field Year is empty or invalid!!!");
	                return false;
	            }
	            else {
	            	System.out.println("All mandatory fields are filled");
	                return true;  }
	    }
	  

	public void saveDescription() {

  	  WebElement saveMovie = wait.until(elementToBeClickable(By.xpath("//input[@id='submit']")));
  	  
  	  saveMovie.click();
		
	}
	
}
